@isTest
private class OutdoorNotInstallControllerTest {
    static testMethod void testMethod1() {
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
        Brand__c brand = new Brand__c(
        	BrandShortName__c = '四川迈克'
        );

        insert brand;
       Province__c province  = new Province__c();
       province.Name = '四川';

       insert province ;

         City__c city = new City__c();
       city.Province__c = province.Id;
       insert city;


         Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');
          acc.Province__c = province.Id;
        acc.City__c = city.Id;
        insert acc;  

          Dealer__c d = new Dealer__c(
        	Only__c='Dealer__c'
        );
         insert d ;


        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;


        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

     	Product__c p  = TestUtils.createInst();
        p.Name = '测试仪器';
        p.RecordTypeId = pRT;
        p.Category0__c  = '仪器';
        p.Category1__c ='生化';
        p.ProductName__c = '测试仪器';
        p.ProductStructure__c = ps1.Id;
        p.CRMBrand__c = brand.Id;
        p.NCCode__c = '10';
        p.InstrumentType__c = '维保仪器';
        insert p ;

        Equipment__c eq = new Equipment__c();
        eq.IMEINumber__c = '0001';
        eq.Product__c = p.Id;
        eq.Account__c = acc.Id;
        eq.OpponentStatus__c = '正常使用';

        insert eq; 


        Equipment__c eq1 = new Equipment__c();
        eq1.IMEINumber__c = '0002';
        eq1.Product__c = p.Id;
        eq1.Account__c = acc.Id;
        eq1.OpponentStatus__c = '安装未使用';

        insert eq1; 

        //仪器组合形式
         CombinationForm__c cf1 = new CombinationForm__c();
        cf1.CombinationForm__c = 'XN-1000';
        cf1.OpponentStatus__c = '安装未使用';
        cf1.Customer__c = acc.Id;
        CombinationForm__c cf2 = new CombinationForm__c();
        cf2.CombinationForm__c = 'XN-1000';
        cf2.Customer__c = acc.Id;
         cf2.OpponentStatus__c = '安装未使用';
        insert new CombinationForm__c[] {cf1, cf2};



        InstrumentProjectOverview__c ipo1 = new InstrumentProjectOverview__c();
        ipo1.Instrument__c = eq.Id;
        ipo1.Items__c= '乙肝';
        ipo1.Status__c = '在用';
        ipo1.TS__c = 1.0;
        ipo1.UsageType__c = '新增';

        insert ipo1;



        Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
       	con.Status__c  = '已生效';
        con.Corpcode__c = 'MKSY';
        con.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalContract', 'Contract__c');
        insert con;


          //报价2
        Quote qt2 = new Quote(Name = '试剂报价',Type__c = '试剂报价',OpportunityId = opp.Id,IsActive__c = true);
        qt2.Status = '已批准';
        qt2.Contract__c = con.Id;
        insert qt2 ;

        QuoteProduct__c qp = new QuoteProduct__c(Name = '测试报价产品2',Product__c = p.Id,Quote__c = qt2.Id,SalesPrice__c = 5);
        insert qp ;


        ContractProduct__c cp = new ContractProduct__c();
        cp.Contract__c = con.Id;
        cp.Product__c =p.Id  ;
        cp.Quantity__c = 5;
        cp.OriginNumber__c = 5;
        cp.SalesPrice__c = 1; 
        cp.ApplicationQuantity__c = 5;
        cp.QuoteProduct__c = qp.Id;
        insert cp ;


         //发货
        InstrumentDeliveryApplication__c ida = new InstrumentDeliveryApplication__c();
        ida.ReceivingCustomer__c = acc.Id;
        ida.DeliveryRelatedContracts__c = con.Id;
        ida.NC__c = true;


        insert ida ;

        InstrumentDeliverynDetails__c idd = new InstrumentDeliverynDetails__c();
        idd.DeliveryInstrument__c = cp.Id;
        idd.Number__c = 1;
        idd.ReturnToMainTable__c = ida.Id;
        insert idd;



        InstallationApply__c ina = new InstallationApply__c();
        ina.Hospital__c = acc.Id;
        ina.InstallInstruments__c = idd.Id;
        ina.InstallEstiDate__c = Date.newInstance(2017, 12, 1);
        ina.InstallsNumber__c = 1;
        ina.EffectiveTime__c = System.now();
        ina.Instrument__c = p.Id;
        ina.NEWDealer__c = d.Id;
        ina.ActualDepotTime__c = System.today(); 

        ina.InstallationType__c = '新装机';
        insert ina; 


        OutdoorNotInstallController oc = new OutdoorNotInstallController();
        oc.init();
        oc.selectProductId = p.Id;
        oc.getEquipDetails();

        oc.getEquipProjectDetails();
        oc.getDetails();
        oc.selectProductId = '安装未使用';
        oc.getCombinationForm();


        List<Object> saveList =new List<Object>();
        Map<String,String> equipProMap = new Map<String,String>();
        
        equipProMap.put('id',ipo1.Id);
        equipProMap.put('ts', '10');
        equipProMap.put('name', '乙肝');
        equipProMap.put('type', '新增');

        saveList.add(equipProMap);


        Map<String,String> equipMap = new Map<String,String>();
        equipMap.put('id',eq.Id);

        Map<String,Object> saveMap = new Map<String,Object>();
        saveMap.put('details', saveList);
        saveMap.put('equips', new List<Object>{equipMap});

        oc.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');
        oc.equipProjectSave();

        saveList.clear();
        Map<String,String> equip = new Map<String,String>();
        equip.put('id',eq.Id);
        equip.put('note', '1x');
        equip.put('reason', 'xxx');
        equip.put('solution', 'xx');
        saveList.add(equip);
        oc.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveList), 'UTF-8');
        oc.equipSave();


        saveList.clear();
        equip.put('id',ina.Id );
      
        saveList.add(equip);
        oc.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveList), 'UTF-8');
       
        oc.setNil();

        List<Object> cfmL = new List<Object>();
        cfmL.add(new OutdoorNotInstallController.CombineBean(cf1));

        oc.saveJson = EncodingUtil.urlEncode(JSON.serialize(cfmL), 'UTF-8');

        oc.combineSave();



    }
}